package com.sqfw.project.community.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqfw.common.utils.DateUtils;
import com.sqfw.common.utils.Query;
import com.sqfw.project.community.domain.*;
import com.sqfw.project.community.domain.vo.WxLoginVO;
import com.sqfw.project.community.service.CommunityService;
import com.sqfw.project.community.service.StatisticsService;
import com.sqfw.project.system.service.ISysUserService;
import com.sqfw.project.wp.service.IWpInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/community/")
public class CommunityController {

    @Autowired
    private CommunityService communityService;
    @Autowired
    private StatisticsService statisticsService;
    @Autowired
    private IWpInfoService wpInfoService;
    @Autowired
    private ISysUserService userService;

    /**
     * 小程序登录接口
     */
    @PostMapping("commonOut/wxLogin")
    public ResultMap wxLogin(@RequestBody WxLoginVO wxLoginVO, HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<>(16);
        resultMap.put("status", 1);
        resultMap.put("msg", "解密成功");
        log.info("小程序登录手机号：{}", wxLoginVO.getPhone());
        // 先数据库查询手机号是否存在
        Map<String, Object> userInfoMap = communityService.wxLogin(wxLoginVO);
        if (CollectionUtils.isEmpty(userInfoMap)) {
            resultMap.put("status", 0);
            resultMap.put("msg", "手机号不存在");
            return ResultMap.ok(resultMap);
        }
        log.info("encryptedData:{}___iv:{}___code:{}", wxLoginVO.getEncryptedData(), wxLoginVO.getIv(), wxLoginVO.getCode());
        if (StringUtils.isNotBlank(wxLoginVO.getCode())) {
            Map<String, Object> userInfo = communityService.getWxUserInfo(wxLoginVO);
            if (CollectionUtils.isEmpty(userInfo)) {
                resultMap.put("status", 0);
                resultMap.put("msg", "解密失败");
                return ResultMap.ok(resultMap);
            }
            resultMap.put("status", 1);
            resultMap.put("msg", "解密成功");
            userInfo.putAll(userInfoMap);

            if (userInfo.get("miniType") != null) {
                resultMap.put("userInfo", userInfo);
            }
            return ResultMap.ok(resultMap);
        } else {
            return ResultMap.error("code 不能为空");
        }

    }


    /**
     * 民政业务
     * startDate
     * endDate
     * unitId
     */
    @PostMapping("statisticsOut/civilStatistis")
    public ResultMap civilStatistis(@RequestBody WpTraceBO wpTrace) {
        List<ServiceLoadBO> list = statisticsService.civilTop(wpTrace);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("civilList", list);
        return ResultMap.ok(resultMap);
    }


    /**
     * 月办结情况
     */
    @PostMapping("statisticsOut/monthHandle")
    public ResultMap monthHandle(@RequestBody WpTraceBO wpTrace) {
        ReplyStatisticsBO replyStatisticsBO = this.statisticsService.replyStatistics(wpTrace);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("monthHandleStatistics", replyStatisticsBO);
        return ResultMap.ok(resultMap);
    }

    /**
     * 12345工单
     */
    @PostMapping("workOrderOut/orderListAppoint")
    public ResultMap orderListAppoint(@RequestBody Map<String, Object> params) {
        Query queryMap = new Query(params);
        if (params.containsKey("reply") && !"undefined".equals(params.get("reply")) && !"".equals(params.get("reply")) && !"null".equals(params.get("reply"))) {
            queryMap.put("isreply", "是".equals(params.get("reply")) ? "1" : "0");
        }

        if (params.containsKey("type") && !"undefined".equals(params.get("type")) && !"".equals(params.get("type")) && !"null".equals(params.get("type"))) {
            queryMap.put("issatisfied", Integer.valueOf(params.get("type").toString()));
        }

        queryMap.put("startDate", params.get("startDate"));
        queryMap.put("unitId", params.get("unitId"));


        return wpInfoService.orderListAppoint(queryMap);
    }

    @PostMapping("workOrderOut/orderInfoAppoint")
    public ResultMap orderInfoAppoint(@RequestBody WpTraceBO wpTrace){
        return wpInfoService.orderInfoAppoint(wpTrace);
    }

    /**
     * 日流转工单
     */
    @PostMapping("statisticsOut/civilAppeal")
    public ResultMap civilAppeal(@RequestBody WpTraceBO wpTrace) {
        wpTrace.setStartDate(LocalDate.parse(wpTrace.getQueryDate()).minusDays(1).toString());
        Map<String, Object> resultMap = statisticsService.civilAppeal(wpTrace);
        return ResultMap.ok(resultMap);
    }


    @PostMapping({"/commonOut/addressGroupList"})
    public ResultMap addressGroupList() {
        Map<String, Object> resultMap = new HashMap<>();
        List<AddressGroupDO> addressGroupList = userService.addressGroup();
        resultMap.put("addressGroupList", addressGroupList);
        return ResultMap.ok(resultMap);
    }

    /**
     * 查询通讯录，部门名称，和手机号
     * */
    @PostMapping({"/commonOut/addressList"})
    public ResultMap addressList(@RequestBody WpTraceBO wpTrace) {
        Map<String, Object> resultMap =  new HashMap<>();
        List<AddressDO> addressList = userService.addressUserList(wpTrace);
        resultMap.put("addressList", addressList);
        return ResultMap.ok(resultMap);
    }

    /**
     * 根据微信授权opedid 获取用户信息
     * */
    @PostMapping("/commonOut/getMiniUserInfo")
    public ResultMap getMiniUserInfo(@RequestBody WpTraceBO wpTrace){
        Map<String, Object> resultMap = new HashMap<>();
        UserInfoVO userInfo = userService.getMiniUserInfo(wpTrace);
        resultMap.put("miniUnitInfo", userInfo);
        return ResultMap.ok(resultMap);
    }
}
